/*
 * @(#)TransferFormatter.java	1.0 2007-11-26
 *
 * Copyright All rights reserved.
 * Use is subject to license terms.
 */
package com.gopay.bis.icbc.bcdl.operation;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.crypto.dsig.TransformException;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.xmlbeans.XmlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infosec.util.Base64;

import com.gopay.bis.common.util.TransUtil;
import com.gopay.bis.common.util.constants.Constants4BankSwitch;
import com.gopay.bis.icbc.bcdl.operation.doc.query.req.CMSDocument;
import com.gopay.common.constants.bank.IcbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.icbc.bcdl.pay.IcbcMessageData;
import com.gopay.common.domain.bank.icbc.bcdl.pay.IcbcPacketMessage;
import com.gopay.common.exception.GopayException;

/**
 * TransferFormatter.java
 * <p>
 * Created on 2007-11-26 Modification history
 * <p>
 * Query transfer information from bank ICBC.
 * 
 * @author Richard Chen
 * @version 1.0
 * @since 1.0
 */

public class Queryformer  {
	/** Logger for logging information. */
	protected Logger log = LoggerFactory.getLogger(this.getClass());
	/** XML文件头. */
	protected static final String XMLHEADER = "<?xml version=\"1.0\" encoding=\"GBK\" ?>\n";

	/** Encoding. */
	protected static final String ENCODING = "GBK";
	protected static final String SUC = "0";
	/** new Initiate capacity. */
	private static final int INIT_CAPACITY = 10;
	/** 版本号. */
	protected String version = null;
	/** 汇款账号. */
	protected String payDepAcc = null;
	/** 收款人地区代码. */
	protected String recAreaCode = null;
	/** 汇款速度. */
	protected String payType = null;
	/** 提交人ID. */
	protected String submitLogId = null;
	/** 企业代码. */
	protected String userId = null;
	/** 汇款单位. */
	protected String payDepName = null;
	/** 付款人地区代码. */
	protected String areaCode = null;
	/** 汇款单位开户行. */
	protected String payDepBranch = null;
	/** 币种. */
	protected String accountCur = null;
	/** 付款人账户地区名. */
	protected String areaName = null;
	/** 账号. */
	protected String acNo = null;
	/** 商城账号. */
	protected String shopacct = null;
	/** 商城类型. */
	protected String shopType = null;
	/** 支付指令查询交易代码. */
	protected String transCode = null;
	/** 财务室指令查询交易代码*/
	protected String qtransCode = null;
	/** b2b交易代码. */
	protected String b2bTransCode = null;
	/** b2c交易代码. */
	protected String b2cTransCode = null;
	/** 集团CIS号. */
	protected String cis = null;
	/** 归属银行编号. */
	protected String bankCode = null;
	/** 证书ID. */
	protected String id = null;
	/** 商城代码. */
	protected String shopCode = null;
	/** 商城账号. */
	protected String shopAcct = null;
	/**批量包查询版本. */
	protected String batchVersion = null;
	
	private static final String XMLBEANSURL = "http://req.query.doc.operation.bcdl.icbc.bis.gopay.com";
	private static final String SEND_XML_NAMESPACE = "http://res.query.doc.operation.bcdl.icbc.bis.gopay.com";
	private static final String RECEIVED_XML_NAMESPACE = "<CMS xmlns=\"http://res.query.doc.operation.bcdl.icbc.bis.gopay.com\">";
	
	private static final String BATCHXMLBEANSURL = "http://req.query.batch.doc.operation.bcdl.icbc.bis.gopay.com";
    private static final String BATCH_SEND_XML_NAMESPACE = "http://res.query.batch.doc.operation.bcdl.icbc.bis.gopay.com";
    private static final String BATCH_RECEIVED_XML_NAMESPACE = "<CMS xmlns=\"http://res.query.batch.doc.operation.bcdl.icbc.bis.gopay.com\">";
    
	public Object format(Object sendMsgData,String id) throws GopayException {
		return doFormat((IcbcMessageData) sendMsgData,id);
	}
	
	public Object packetFormat(IcbcPacketMessage message,String id) throws GopayException {
        try{
            message.setVersion(this.batchVersion);
            message.setTransCode(this.transCode);
            message.setCis(this.cis);
            message.setBankCode(this.bankCode);
            message.setId(id);

            com.gopay.bis.icbc.bcdl.operation.doc.batch.query.req.CMSDocument doc = com.gopay.bis.icbc.bcdl.operation.doc.batch.query.req.CMSDocument.Factory
                    .newInstance();
            com.gopay.bis.icbc.bcdl.operation.doc.batch.query.req.CMSDocument.CMS cms = doc.addNewCMS();
            com.gopay.bis.icbc.bcdl.operation.doc.batch.query.req.CMSDocument.CMS.Eb eb = cms.addNewEb();
            com.gopay.bis.icbc.bcdl.operation.doc.batch.query.req.CMSDocument.CMS.Eb.Pub pub = eb.addNewPub();
            com.gopay.bis.icbc.bcdl.operation.doc.batch.query.req.CMSDocument.CMS.Eb.In in = eb.addNewIn();

            pub.setTransCode(message.getTransCode());
            pub.setCIS(message.getCis());
            pub.setBankCode(message.getBankCode());
            pub.setID(message.getId());
            pub.setTranDate(getSpaceIfEmpty(message.getTranDate()));
            pub.setTranTime(getSpaceIfEmpty(message.getTranTime()));
            pub.setFSeqno(getSpaceIfEmpty(message.getPacketNo()));

            in.setQryfSeqno(getSpaceIfEmpty(message.getPacketNo()));
            in.setQrySerialNo(getSpaceIfEmpty(message.getQrySerialNo()));
            in.setReqReserved1(getSpaceIfEmpty(message.getReqReserved1()));
            in.setReqReserved2(getSpaceIfEmpty(message.getReqReserved2()));
            
            List<IcbcMessageData> queryDatas = message.getIcbcMessageDatas();
            if (null != queryDatas && queryDatas.size() > 0) {
                for (int i = 0; i < queryDatas.size(); i++) {
                    // 循环增加rd节点
                    com.gopay.bis.icbc.bcdl.operation.doc.batch.query.req.CMSDocument.CMS.Eb.In.Rd rd = in.addNewRd();
                    IcbcMessageData msg = queryDatas.get(i);
                    rd.setISeqno(getSpaceIfEmpty(msg.getBankOrderId()));
                    rd.setQryiSeqno(getSpaceIfEmpty(msg.getBankOrderId()));
                    rd.setQryOrderNo(getSpaceIfEmpty(msg.getQryOrderNo()));
                    rd.setReqReserved3(getSpaceIfEmpty(msg.getReqReserved3()));
                    rd.setReqReserved4(getSpaceIfEmpty(msg.getReqReserved4()));
                }
            }
            
            StringBuffer xmlString = new StringBuffer(XMLHEADER);
            xmlString.append(doc.xmlText(XmlUtil.buildFormatXmlOptions(BATCHXMLBEANSURL)));
            log.info("packet message to be sent: \n" + xmlString.toString());
//            msg.setOpName("NCQPayentOp");
            return xmlString.toString().getBytes("UTF-8");
        }catch(Exception e){
            log.error("Queryformer packetFormat error: ", e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6050);
        }
    }
	
	/**
	 * Parse message.
	 * 
	 * @param receivedMsgData
	 *            应答报文.
	 * @return MessageDataCCB 建行数据结构体.
	 * @throws TransformException
	 *             报文格式错误.
	 */
	public Object parse(Object receivedMsgData) throws GopayException {
		String msg = (String) receivedMsgData;
		
		try {
			msg = new String(msg.getBytes("UTF-8"), "GBK");

			String plainMsg = "";
			plainMsg = new String(Base64.decode(msg),"GBK");
			log.info("ICBC Receive XmlString: \n"
					+ plainMsg);
			String returnCode = TransUtil.getMsgField(
					IcbcConstants.RETURN_CODE_BEGIN_TAG_NEW,
					IcbcConstants.RETURN_CODE_END_TAG_NEW, plainMsg);
			if (SUC.equals(returnCode)) {
				return doParse(plainMsg);
			} else {
				IcbcMessageData tranMsg = new IcbcMessageData();
				String returnMsg = TransUtil.getMsgField(
						IcbcConstants.RETURN_MSG_BEGIN_TAG_NEW,
						IcbcConstants.RETURN_MSG_END_TAG_NEW, plainMsg);
				tranMsg.setRetCode(StringUtils.isEmpty(returnCode) ? Constants4BankSwitch.DEFAULT_RETURN_CODE
						: returnCode);
				tranMsg.setErrMsg(StringUtils.isEmpty(returnMsg) ? Constants4BankSwitch.DEFAULT_RETURN_MSG
						: returnMsg);
				return tranMsg;
				
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6052);
		} catch (XmlException e) {
			e.printStackTrace();
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6052);
		}
	}
	
    /**
     * 工行批量接口查询返回报文解析
     * 
     * @param receivedMsgData
     * @return
     * @throws GopayException
     */
    public Object packetDataParse(Object receivedMsgData) throws GopayException {
        String msg = (String) receivedMsgData;
        
        try {
            msg = new String(msg.getBytes("UTF-8"), "GBK");

            String plainMsg = "";
            plainMsg = new String(Base64.decode(msg),"GBK");
            log.info("ICBC packet query Receive XmlString: \n"
                    + LoggerMaskUtils.maskString(plainMsg));
            String returnCode = TransUtil.getMsgField(
                    IcbcConstants.RETURN_CODE_BEGIN_TAG_NEW,
                    IcbcConstants.RETURN_CODE_END_TAG_NEW, plainMsg);
            if (SUC.equals(returnCode)) {
                return doPacketParse(plainMsg);
            } else {
                IcbcPacketMessage tranMsg = new IcbcPacketMessage();
                String returnMsg = TransUtil.getMsgField(
                        IcbcConstants.RETURN_MSG_BEGIN_TAG_NEW,
                        IcbcConstants.RETURN_MSG_END_TAG_NEW, plainMsg);
                tranMsg.setRetCode(StringUtils.isEmpty(returnCode) ? Constants4BankSwitch.DEFAULT_RETURN_CODE
                        : returnCode);
                tranMsg.setRetMsg(StringUtils.isEmpty(returnMsg) ? Constants4BankSwitch.DEFAULT_RETURN_MSG
                        : returnMsg);
                return tranMsg;
            }
        } catch (IOException e) {
            log.error("Queryformer packetDataParse error: ", e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6052);
        } catch (XmlException e) {
            log.error("Queryformer packetDataParse error: ", e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6052);
        }
    }
	
	public Object doFormat(IcbcMessageData msg,String id) throws GopayException {
		try{
			msg.setVersion(this.version);
			msg.setTransCode(this.transCode);
			msg.setCis(this.cis);
			msg.setBankCode(this.bankCode);
			msg.setId(id);

			CMSDocument doc = CMSDocument.Factory.newInstance();
			CMSDocument.CMS cms = doc.addNewCMS();
			CMSDocument.CMS.Eb eb = cms.addNewEb();
			CMSDocument.CMS.Eb.Pub pub = eb.addNewPub();
			CMSDocument.CMS.Eb.In in = eb.addNewIn();

			pub.setTransCode(msg.getTransCode());
			pub.setCIS(msg.getCis());
			pub.setBankCode(msg.getBankCode());
			pub.setID(msg.getId());
			pub.setTranDate(getSpaceIfEmpty(msg.getTranDate()));
			pub.setTranTime(getSpaceIfEmpty(msg.getTranTime()));
			pub.setFSeqno(getSpaceIfEmpty(msg.getfSeqno()));

			in.setQryfSeqno(getSpaceIfEmpty(msg.getQryfSeqno()));
			in.setQrySerialNo(getSpaceIfEmpty(msg.getQrySerialNo()));
			StringBuffer xmlString = new StringBuffer(XMLHEADER);
			xmlString.append(doc.xmlText(XmlUtil.buildFormatXmlOptions(XMLBEANSURL)));
			log.info("Message to be sent: \n" + xmlString.toString());
			msg.setOpName("NCQPayentOp");
			return xmlString.toString().getBytes("UTF-8");
		}catch(Exception e){
			e.printStackTrace();
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6050);
		}
	}

	protected Object doParse(String msg) throws XmlException {
		log.info("receive Message: \n" + msg.toString());

		com.gopay.bis.icbc.bcdl.operation.doc.query.res.CMSDocument doc = null;
			doc = com.gopay.bis.icbc.bcdl.operation.doc.query.res.CMSDocument.Factory
					.parse(msg.replaceFirst(IcbcConstants.CMS_BEGIN_TAG,
							RECEIVED_XML_NAMESPACE), XmlUtil
							.buildParseXmlOptions(SEND_XML_NAMESPACE));
		com.gopay.bis.icbc.bcdl.operation.doc.query.res.CMSDocument.CMS res = doc.getCMS();
		com.gopay.bis.icbc.bcdl.operation.doc.query.res.CMSDocument.CMS.Eb eb = res.getEb();
		com.gopay.bis.icbc.bcdl.operation.doc.query.res.CMSDocument.CMS.Eb.Pub pub = eb
				.getPub();
		com.gopay.bis.icbc.bcdl.operation.doc.query.res.CMSDocument.CMS.Eb.Out out = eb
				.getOut();
		com.gopay.bis.icbc.bcdl.operation.doc.query.res.CMSDocument.CMS.Eb.Out.Rd rd = out
				.getRd();

		IcbcMessageData tranMsg = new IcbcMessageData();
		tranMsg.setTransCode(pub.getTransCode());
		tranMsg.setCis(pub.getCIS());
		tranMsg.setBankCode(pub.getBankCode());
		tranMsg.setId(pub.getID());
		tranMsg.setTranDate(pub.getTranDate());
		tranMsg.setTranTime(pub.getTranTime());
		tranMsg.setfSeqno(pub.getFSeqno());
		tranMsg.setRetCode(pub.getRetCode());
		tranMsg.setRetMsg(pub.getRetMsg());

		tranMsg.setQryfSeqno(out.getQryfSeqno());
		tranMsg.setQrySerialNo(out.getQrySerialNo());
		tranMsg.setOnlBatF(out.getOnlBatF());
		tranMsg.setSettleMode(out.getSettleMode());
		tranMsg.setBusType(out.getBusType());

		if (rd == null) {
			tranMsg.setResult(IcbcConstants.NCPAYENT_STATUS_NO_FOUND);
			return tranMsg;
		}
		tranMsg.setISeqno(rd.getISeqno());
		tranMsg.setQryiSeqno(rd.getQryiSeqno());
		tranMsg.setQryOrderNo(rd.getQryOrderNo());
		tranMsg.setReimburseNo(rd.getReimburseNo());
		tranMsg.setReimburseNum(rd.getReimburseNum());
		tranMsg.setStartDate(rd.getStartDate());
		tranMsg.setStartTime(rd.getStartTime());
		tranMsg.setPayType(rd.getPayType());
		tranMsg.setPayAccNo(rd.getPayAccNo());
		tranMsg.setPayAccNameCN(rd.getPayAccNameCN());
		tranMsg.setPayAccNameEN(rd.getPayAccNameEN());
		tranMsg.setRecAccNo(rd.getRecAccNo());
		tranMsg.setRecAccNameCN(rd.getRecAccNameCN());
		tranMsg.setRecAccNameEN(rd.getRecAccNameEN());
		tranMsg.setSysIOFlg(rd.getSysIOFlg());
		tranMsg.setSameCity(rd.getIsSameCity());
		tranMsg.setRecICBCCode(rd.getRecICBCCode());
		tranMsg.setRecCityName(rd.getRecCityName());
		tranMsg.setRecBankNo(rd.getRecBankNo());
		tranMsg.setRecBankName(rd.getRecBankName());
		tranMsg.setCurrType(rd.getCurrType());
		tranMsg.setPayAmt(rd.getPayAmt());
		tranMsg.setUseCode(rd.getUseCode());
		tranMsg.setUseCN(rd.getUseCN());
		tranMsg.setEnSummary(rd.getEnSummary());
		tranMsg.setPostScript(rd.getPostScript());
		tranMsg.setSummary(rd.getSummary());
		tranMsg.setRef(rd.getRef());
		tranMsg.setOref(rd.getOref());
		tranMsg.setERPSqn(rd.getERPSqn());
		tranMsg.setBusCode(rd.getBusCode());
		tranMsg.setERPcheckno(rd.getERPcheckno());
		tranMsg.setCrvouhType(rd.getCrvouhType());
		tranMsg.setCrvouhName(rd.getCrvouhName());
		tranMsg.setCrvouhNo(rd.getCrvouhNo());
		tranMsg.setBankType(rd.getBankType());
		tranMsg.setFileNames(rd.getFileNames());
		tranMsg.setIndexs(rd.getIndexs());
		tranMsg.setPaySubNo(rd.getPaySubNo());
		tranMsg.setRecSubNo(rd.getRecSubNo());
		tranMsg.setIRetCode(rd.getIRetCode());
		tranMsg.setIRetMsg(rd.getIRetMsg());
		tranMsg.setResult(rd.getResult());
		tranMsg.setInstrRetCode(rd.getInstrRetCode());
		tranMsg.setInstrRetMsg(rd.getInstrRetMsg()==null?"":rd.getInstrRetMsg().trim());
		tranMsg.setBankRetTime(rd.getBankRetTime());

		return tranMsg;
	}
	
	protected Object doPacketParse(String msg) throws XmlException {
        log.info("packet query receive Message: \n" + LoggerMaskUtils.maskString(msg.toString()));

        com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument doc = null;
        doc = com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument.Factory.parse(
                msg.replaceFirst(IcbcConstants.CMS_BEGIN_TAG, BATCH_RECEIVED_XML_NAMESPACE),
                XmlUtil.buildParseXmlOptions(BATCH_SEND_XML_NAMESPACE));
        com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument.CMS res = doc.getCMS();
        com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument.CMS.Eb eb = res.getEb();
        com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument.CMS.Eb.Pub pub = eb.getPub();
        com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument.CMS.Eb.Out out = eb.getOut();
        com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument.CMS.Eb.Out.Rd[] rds = out.getRdArray();

        IcbcPacketMessage tranMsg = new IcbcPacketMessage();
        tranMsg.setTransCode(pub.getTransCode());
        tranMsg.setCis(pub.getCIS());
        tranMsg.setBankCode(pub.getBankCode());
        tranMsg.setId(pub.getID());
        tranMsg.setTranDate(pub.getTranDate());
        tranMsg.setTranTime(pub.getTranTime());
        tranMsg.setfSeqno(pub.getFSeqno());
        tranMsg.setRetCode(pub.getRetCode());
        tranMsg.setRetMsg(pub.getRetMsg());

        tranMsg.setQryfSeqno(out.getQryfSeqno());
        tranMsg.setQrySerialNo(out.getQrySerialNo());
        tranMsg.setOnlBatF(out.getOnlBatF());
        tranMsg.setSettleMode(out.getSettleMode());
        tranMsg.setBusType(out.getBusType());

        if (null == rds || rds.length == 0) {
            tranMsg.setRetCode(IcbcConstants.NCPAYENT_STATUS_NO_FOUND);
            return tranMsg;
        }

        List<IcbcMessageData> messageList = new ArrayList<IcbcMessageData>(); // 批量查询接口返回list
        for (int i = 0; i < rds.length; i++) {
            // 循环处理批量查询返回结果
            com.gopay.bis.icbc.bcdl.operation.doc.batch.query.res.CMSDocument.CMS.Eb.Out.Rd rd = rds[i];
            IcbcMessageData message = new IcbcMessageData();
            message.setISeqno(rd.getISeqno());
            message.setQryiSeqno(rd.getQryiSeqno());
            message.setQryOrderNo(rd.getQryOrderNo());
            message.setReimburseNo(rd.getReimburseNo());
            message.setReimburseNum(rd.getReimburseNum());
            message.setStartDate(rd.getStartDate());
            message.setStartTime(rd.getStartTime());
            message.setPayType(rd.getPayType());
            message.setPayAccNo(rd.getPayAccNo());
            message.setPayAccNameCN(rd.getPayAccNameCN());
            message.setPayAccNameEN(rd.getPayAccNameEN());
            message.setRecAccNo(rd.getRecAccNo());
            message.setRecAccNameCN(rd.getRecAccNameCN());
            message.setRecAccNameEN(rd.getRecAccNameEN());
            message.setSysIOFlg(rd.getSysIOFlg());
            message.setSameCity(rd.getIsSameCity());
            message.setRecICBCCode(rd.getRecICBCCode());
            message.setRecCityName(rd.getRecCityName());
            message.setRecBankNo(rd.getRecBankNo());
            message.setRecBankName(rd.getRecBankName());
            message.setCurrType(rd.getCurrType());
            message.setPayAmt(rd.getPayAmt());
            message.setUseCode(rd.getUseCode());
            message.setUseCN(rd.getUseCN());
            message.setEnSummary(rd.getEnSummary());
            message.setPostScript(rd.getPostScript());
            message.setSummary(rd.getSummary());
            message.setRef(rd.getRef());
            message.setOref(rd.getOref());
            message.setERPSqn(rd.getERPSqn());
            message.setBusCode(rd.getBusCode());
            message.setERPcheckno(rd.getERPcheckno());
            message.setCrvouhType(rd.getCrvouhType());
            message.setCrvouhName(rd.getCrvouhName());
            message.setCrvouhNo(rd.getCrvouhNo());
            message.setIRetCode(rd.getIRetCode());
            message.setIRetMsg(rd.getIRetMsg());
            message.setResult(rd.getResult());
            message.setInstrRetCode(rd.getInstrRetCode());
            message.setInstrRetMsg(rd.getInstrRetMsg() == null ? "" : rd.getInstrRetMsg().trim());
            message.setBankRetTime(rd.getBankRetTime());
            
            messageList.add(message);
        }
        tranMsg.setIcbcMessageDatas(messageList);
        return tranMsg;
    }
	
	/**
	 * 取得发送NetSafeClient的请求参数.
	 * 
	 * @param obj
	 *            报文.
	 * @param formatRes
	 *            format res.
	 * @return map.
	 */
	public Map getIssueNCParaMap(Object obj, Object formatRes)throws GopayException {
		byte[] formatResByte = (byte[]) formatRes;
		try {
			formatResByte = new String(formatResByte).getBytes("GBK");
		
		IcbcMessageData msg = (IcbcMessageData) obj;
		String reqData = null;
		reqData = new String(formatResByte);
		
		// 组织提交工行所需form的field域及其值，报文体由IcbcConstants.REQ_DATA指定
		Map requestParaMap = new HashMap(INIT_CAPACITY);
		// 证书ID
		requestParaMap.put(IcbcConstants.USER_ID, msg.getId());
		// 请求时间
		requestParaMap.put(IcbcConstants.SEND_TIME, TransUtil.getSendTime());
		// 版本号（不同版本号对应的接口格式不同，请参考具体交易的接口文档)
		requestParaMap.put(IcbcConstants.VERSION, msg.getVersion());
		// 交易码
		requestParaMap.put(IcbcConstants.TRANS_CODE, msg.getTransCode());
		// 客户的归属单位
		requestParaMap.put(IcbcConstants.BANK_CODE, msg.getBankCode());
		// 客户的归属编码
		requestParaMap.put(IcbcConstants.GROUP_CIS, msg.getCis());
		// 客户的证书ID（无证书客户可空)
		requestParaMap.put(IcbcConstants.ID, msg.getId());
		// 客户的指令包序列号（由客户ERP系统产生，不可重复)
		requestParaMap.put(IcbcConstants.PACKAGE_ID, msg.getfSeqno());
		// 客户的证书公钥信息（进行BASE64编码；NC客户送空)
		requestParaMap.put(IcbcConstants.CERT, "");
		// 客户的xml请求数据,由相应的Formatter生成
		requestParaMap.put(IcbcConstants.REQ_DATA, reqData);
		return requestParaMap;
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6050);
		}
	}
	
    /**
     * 取得批量发送NetSafeClient的请求参数.
     * 
     * @param obj
     * @param formatRes
     * @return
     * @throws GopayException
     */
    public Map getPacketIssueNCParaMap(Object obj, Object formatRes)throws GopayException {
        byte[] formatResByte = (byte[]) formatRes;
        try {
            formatResByte = new String(formatResByte).getBytes("GBK");
        
        IcbcPacketMessage msg = (IcbcPacketMessage) obj;
        String reqData = null;
        reqData = new String(formatResByte);
        
        // 组织提交工行所需form的field域及其值，报文体由IcbcConstants.REQ_DATA指定
        Map requestParaMap = new HashMap(INIT_CAPACITY);
        // 证书ID
        requestParaMap.put(IcbcConstants.USER_ID, msg.getId());
        // 请求时间
        requestParaMap.put(IcbcConstants.SEND_TIME, TransUtil.getSendTime());
        // 版本号（不同版本号对应的接口格式不同，请参考具体交易的接口文档)
        requestParaMap.put(IcbcConstants.VERSION, msg.getVersion());
        // 交易码
        requestParaMap.put(IcbcConstants.TRANS_CODE, msg.getTransCode());
        // 客户的归属单位
        requestParaMap.put(IcbcConstants.BANK_CODE, msg.getBankCode());
        // 客户的归属编码
        requestParaMap.put(IcbcConstants.GROUP_CIS, msg.getCis());
        // 客户的证书ID（无证书客户可空)
        requestParaMap.put(IcbcConstants.ID, msg.getId());
        // 客户的指令包序列号（由客户ERP系统产生，不可重复)
        requestParaMap.put(IcbcConstants.PACKAGE_ID, msg.getPacketNo());
        // 客户的证书公钥信息（进行BASE64编码；NC客户送空)
        requestParaMap.put(IcbcConstants.CERT, "");
        // 客户的xml请求数据,由相应的Formatter生成
        requestParaMap.put(IcbcConstants.REQ_DATA, reqData);
        return requestParaMap;
        } catch (UnsupportedEncodingException e1) {
            log.error("Queryformer getPacketIssueNCParaMap error: ", e1);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6050);
        }
    }
	
	/**
	 * 当参数值是empty时返回空格
	 */
	public String getSpaceIfEmpty(String value) {
		return StringUtils.isEmpty(value) ? " " : value;
	}
	
	/**
	 * Set account currency.
	 * 
	 * @param accountCur
	 *            the accountCur to set.
	 */
	public void setAccountCur(String accountCur) {
		this.accountCur = accountCur;
	}

	/**
	 * Set area code.
	 * 
	 * @param areaCode
	 *            the areaCode to set.
	 */
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	/**
	 * Set area name.
	 * 
	 * @param areaName
	 *            the areaName to set.
	 */
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	/**
	 * Set pay deptment branch.
	 * 
	 * @param payDepBranch
	 *            the payDepBranch to set.
	 */
	public void setPayDepBranch(String payDepBranch) {
		this.payDepBranch = payDepBranch;
	}

	/**
	 * Set pay deptment name.
	 * 
	 * @param payDepName
	 *            the payDepName to set.
	 */
	public void setPayDepName(String payDepName) {
		this.payDepName = payDepName;
	}

	/**
	 * Set pay type.
	 * 
	 * @param payType
	 *            the payType to set.
	 */
	public void setPayType(String payType) {
		this.payType = payType;
	}

	/**
	 * Set receive area code.
	 * 
	 * @param recAreaCode
	 *            the recAreaCode to set.
	 */
	public void setRecAreaCode(String recAreaCode) {
		this.recAreaCode = recAreaCode;
	}

	/**
	 * Set pay deptment account.
	 * 
	 * @param payDepAcc
	 *            the payDepAcc to set.
	 */
	public void setPayDepAcc(String payDepAcc) {
		this.payDepAcc = payDepAcc;
	}

	/**
	 * Set submit log id.
	 * 
	 * @param submitLogId
	 *            the submitLogId to set.
	 */
	public void setSubmitLogId(String submitLogId) {
		this.submitLogId = submitLogId;
	}

	/**
	 * Set user id.
	 * 
	 * @param userID
	 *            the userId to set.
	 */
	public void setUserId(String userID) {
		this.userId = userID;
	}

	/**
	 * Set ac no.
	 * 
	 * @param acNo
	 *            the acNo to set
	 */
	public void setAcNo(String acNo) {
		this.acNo = acNo;
	}


	public String getShopacct() {
		return shopacct;
	}

	public void setShopacct(String shopacct) {
		this.shopacct = shopacct;
	}

	public String getPayDepAcc() {
		return payDepAcc;
	}

	public String getRecAreaCode() {
		return recAreaCode;
	}

	public String getPayType() {
		return payType;
	}

	public String getSubmitLogId() {
		return submitLogId;
	}

	public String getUserId() {
		return userId;
	}

	public String getPayDepName() {
		return payDepName;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public String getPayDepBranch() {
		return payDepBranch;
	}

	public String getAccountCur() {
		return accountCur;
	}

	public String getAreaName() {
		return areaName;
	}

	public String getAcNo() {
		return acNo;
	}

	public String getCis() {
		return cis;
	}

	public void setCis(String cis) {
		this.cis = cis;
	}

	public String getBankCode() {
		return bankCode;
	}

	public void setBankCode(String bankCode) {
		this.bankCode = bankCode;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getShopAcct() {
		return shopAcct;
	}

	public void setShopAcct(String shopAcct) {
		this.shopAcct = shopAcct;
	}

	public static String getXmlheader() {
		return XMLHEADER;
	}

	public static String getEncoding() {
		return ENCODING;
	}

	public String getShopType() {
		return shopType;
	}

	public void setShopType(String shopType) {
		this.shopType = shopType;
	}

	public String getTransCode() {
		return transCode;
	}

	public void setTransCode(String transCode) {
		this.transCode = transCode;
	}

	public String getQtransCode() {
		return qtransCode;
	}
	public void setQtransCode(String qtransCode) {
		this.qtransCode = qtransCode;
	}
	public String getB2bTransCode() {
		return b2bTransCode;
	}

	public void setB2bTransCode(String b2bTransCode) {
		this.b2bTransCode = b2bTransCode;
	}

	public String getB2cTransCode() {
		return b2cTransCode;
	}

	public void setB2cTransCode(String b2cTransCode) {
		this.b2cTransCode = b2cTransCode;
	}

	public String getShopCode() {
		return shopCode;
	}

	public void setShopCode(String shopCode) {
		this.shopCode = shopCode;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

    public String getBatchVersion() {
        return batchVersion;
    }

    public void setBatchVersion(String batchVersion) {
        this.batchVersion = batchVersion;
    }
	
}
